// pages/Supermembers/Supermembers.js
const app = getApp()

var Tool = require('../../config/tool.js');

//团购列表  分类背景
import { getCatGoryList, getPageInfo } from '../../config/https'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    groupsset:'',         //背景
    category:'',          //分类
    height:'',
    showTop: false,
    page:0,
    time: '',
    list:[],            //列表数据
    loadMore: false,    //加载中提示
    finished: false,    //是否已经请求完全部数据
    newestTime: ''      //最新的时间戳

  },
  // 跳转详情
  onDirectTo(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/directbuyingto/directbuyingto?id='+id,
    })
  },

  onLoad: function (options) {//生命周期函数--监听页面加载
    let that = this
    setInterval(function(){
      that.setData({newestTime: Date.parse(new Date()) / 1000})
    },1000)
    this.onShowData()
  },

  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth - 20; //获取当前屏幕的宽度
    var imgh = e.detail.height;//图片高度
    var imgw = e.detail.width;//图片宽度
    var swiperH = winWid * imgh / imgw + "px"
    this.setData({
      height: swiperH//设置高度
    })
  },
  onReady: function () {//生命周期函数--监听页面初次渲染完成

  },


  onShow: function () {//生命周期函数--监听页面显示
    this.onList()
  },
  //分类改变
  onChange(e){
    this.setData({ page: 0, list: [] })
    let category = this.data.category
    let index = e.detail.index
    wx.showLoading({
      title: '加载中...',
    })
    clearInterval(this.time);
    this.onList(category[index].id)
    
  },
  //获取分类和背景
  onShowData(){
    var that = this
    getPageInfo({}).then(res=>{
      console.log(res.category)
      that.setData({
        groupsset: res.groupsset,
        category: res.category
      })
    })
  },
  //列表数据
  onList(id) {
    var that = this
    that.setData({
      loadMore: true
    })
    getCatGoryList({
      page:++that.data.page,
      category: id?id:''
    }).then(res=>{  
      wx.hideLoading()
      var list = res.list
      var timestamp = Date.parse(new Date()) / 1000;
      list.forEach(item => {
        item.text = '',
        item.last = item.grouptimeend - timestamp,
        item.startTime = Tool.u_formatTimestamp(item.grouptimestart*1000,{type:'dhm'})
      })
      list = that.data.list.concat(list)
      that.setData({
        loadMore: false
      })
      if (list.length == 0) {
      } else {
        that.setData({
          finished: res.list.length < res.pagesize ? true : false
        })
      }
      clearInterval(that.time);
      wx.stopPullDownRefresh();
      //团购倒计时开始
      that.time= setInterval(() => {
        list.forEach(item => {
          item.text = Tool.count(item.last ? item.last : 0);
          if (Number(item.last) > 0) {
              item.last--;
          } else {
            // clearInterval(this.time)
          }
        })
        
        that.setData({
          list
        })
      }, 1000);
      
    }).catch(err=>{
      app.$tip(err.message)
      wx.hideLoading()
    })
  },
  onHide: function () {//生命周期函数--监听页面隐藏

  },


  onUnload: function () {//生命周期函数--监听页面卸载

  },


  onPullDownRefresh: function () {//页面相关事件处理函数--监听用户下拉动作

  },


  onReachBottom: function () {//页面上拉触底事件的处理函数
    if (!this.data.finished && !this.data.loadMore) {
      this.onList()
    }
  },


  onShareAppMessage: function () {//用户点击右上角分享
    return {
      title: '满足顾客需求，创造精彩生活 限时拼团购，等你来抢哟~',
      path: '/pages/directbuying/directbuying?userid=' + wx.getStorageSync('userInfo'), 
      imageUrl: '../../img/3.jpg',  //转发图片 默认当前页面图片
      success: (res) => {//转发成功
        console.log("转发成功", res);
      }
    }
  }
})